import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'color.dart';
import 'log_util.dart';

class StringPicker extends StatefulWidget {
  String? title;

  //数据
  List<String>? data = [];
  int? currentIndex = 0;
  // 选中的地址发生改变回调
  final ValueChanged<int>? onSelectedStringChanged;
  // 选中的地址选择完成回调
  final ValueChanged<int>? onSelectedStringCompleted;
  // 文字显示样式
  final TextStyle? style;

  StringPicker({
    Key? key,
    this.title,
    this.data,
    this.onSelectedStringChanged,
    this.onSelectedStringCompleted,
    this.style,
    this.currentIndex,
  }) : super(key: key);
  @override
  _StringPickerState createState() => _StringPickerState();
}

class _StringPickerState extends State<StringPicker> {
  int? _currenSelectIndex;

  @override
  void initState() {
    super.initState();
    _currenSelectIndex = widget.currentIndex ?? 0;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      height: 294.w,
      child: Column(
        children: [
          barView(context,
              title: widget.title ?? "",
              cancelCallBack: () => Get.back(),
              doneCallBack: () {
                Get.back();
                if (widget.onSelectedStringCompleted != null) {
                  widget.onSelectedStringCompleted!(_currenSelectIndex!);
                }
              }),
          Expanded(child: contentView()),
        ],
      ),
    );
  }

  Widget contentView() {
    return Row(
      children: <Widget>[
        Expanded(
          flex: 1,
          child: CupertinoPicker.builder(
            scrollController:
                FixedExtentScrollController(initialItem: _currenSelectIndex!),
            backgroundColor: Colors.white,
            childCount: widget.data?.length,
            itemBuilder: (context, index) {
              var item = widget.data?[index];
              return Container(
                alignment: Alignment.center,
                child: AutoSizeText(
                  item!,
                  style: widget.style,
                  maxLines: 1,
                ),
              );
            },
            itemExtent: 44.w,
            onSelectedItemChanged: (index) {
              LogUtils.d("index = $index");
              setState(() {
                _currenSelectIndex = index;
              });
              _updateCurrent();
            },
          ),
        ),
      ],
    );
  }

  void _updateCurrent() {
    if (widget.onSelectedStringChanged != null) {
      widget.onSelectedStringChanged!(_currenSelectIndex ?? 0);
    }
  }
}

Widget barView(BuildContext context,
    {String? title, VoidCallback? cancelCallBack, VoidCallback? doneCallBack}) {
  return Container(
    height: 44.w,
    color: const Color(0xffF6F6F6),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        GestureDetector(
          onTap: cancelCallBack,
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 7.w),
            child: Text(
              "取消",
              textAlign: TextAlign.center,
              style: TextStyle(color: Colors.black, fontSize: 15.sp),
            ),
          ),
        ),
        Text(
          title ?? "",
          style: TextStyle(color: ColorManager.color000000, fontSize: 15.sp),
          textAlign: TextAlign.center,
        ),
        GestureDetector(
          onTap: doneCallBack,
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 7.w),
            child: Text(
              "确定",
              style: TextStyle(color: Colors.black, fontSize: 15.sp),
              textAlign: TextAlign.center,
            ),
          ),
        ),
      ],
    ),
  );
}
